#include <iostream>
#include "DoubleLinkList.h"

using namespace std;

// 拷贝构造函数
template<typename T>
DoubleLinkList<T>::DoubleLinkList(const DoubleLinkList<T>& list)
	:_size(0), _pHead(nullptr), _pTail(nullptr)
{
	Node<T>* pTemp = list._pHead;
	while (pTemp)
	{
		PushBack(pTemp->_value);
		pTemp = pTemp->_pNext;
	}
}
 
// 构造函数重载
template<typename T>
DoubleLinkList<T>::DoubleLinkList(size_t n, const T& value = T())
	:_size(0), _pHead(nullptr), _pTail(nullptr)
{
	for (int idx = 0; idx < (int)n; idx++)
	{
		PushBack(value);
	}
}
 
// 赋值操作符重载
template<typename T>
DoubleLinkList<T>& DoubleLinkList<T>::operator =(const DoubleLinkList<T>& list)
{
	if (this != &list)
	{
		_Destroy();
		Node<T>* pTemp =list._pHead;
		while (pTemp)
		{
			PushBack(pTemp->_value);
			pTemp = pTemp->_pNext;
		}
	}
	return *this;
}
 
// 输出运算符重载
template<class T>
ostream& operator<<(ostream &_cout, const DoubleLinkList<T>&list)
{
	if (list._pHead == nullptr)
	{
		cout << "链表以空。" << endl;
	}
	Node<T>* pCur = list._pHead;
	while (pCur)
	{
		_cout<< "  " <<pCur->_value;
		pCur = pCur->_pNext;
	}
	return _cout;
}
 
// 析构函数
template<typename T>
DoubleLinkList<T>::~DoubleLinkList()
{
	_Destroy();
}
 
// 尾插
template<typename T>
void DoubleLinkList<T>::PushBack(const T& value)
{
	if (nullptr == _pHead)
	{
		_pHead = _pTail = _BuyNode(value);
	}
	else
	{
		_pTail->_pNext = _BuyNode(value);
		_pTail->_pNext->_pPre = _pTail;
		_pTail = _pTail->_pNext;
	}
	++_size;
}
 
// 尾删
template<typename T>
void DoubleLinkList<T>::PopBack()
{
	if (nullptr == _pHead)
	{
		cout <<" 删除失败，链表以空。"<<endl;
		return;
	}
	else if(1 == _size)
	{
		delete _pHead;
		_pHead = _pTail = nullptr;
		--_size;
	}
	else
	{
		_pTail = _pTail->_pPre;
		delete _pTail->_pNext;
		_pTail->_pNext = nullptr;
		--_size;
	}
}
 
// 头插
template<typename T>
void DoubleLinkList<T>::PushFront(const T& value)
{
	if ( nullptr == _pHead)
	{
		_pHead = _pTail = _BuyNode(value);
	}
	else
	{
		_pHead->_pPre = _BuyNode(value);
		_pHead->_pPre->_pNext = _pHead;
		_pHead = _pHead->_pPre;
	}
	++_size;
}
 
// 头删
template<typename T>
void DoubleLinkList<T>::PopFront()
{
	if (nullptr == _pHead)
	{
		cout <<" 删除失败，链表以空。"<<endl;
		return;
	}
	else if(1 == _size)
	{
		delete _pTail;
		_pHead = _pTail = nullptr;
		--_size;
	}
	else
	{
		_pHead = _pHead->_pNext;
		delete _pHead->_pPre;
		_pHead->_pPre = nullptr;
		--_size;
	}
}
 
// 查找
template <typename T>
Node<T>* DoubleLinkList<T>::Find(const T&value)
{
	Node<T>* temp = _pHead;
	while (temp)
	{
		if (temp->_value == value)
		{
			return temp;
		}
		temp = temp->_pNext;
	}
	cout << "此元素不存在。" << endl;
	return nullptr;
}
 
// 插入
template<typename T>
void DoubleLinkList<T>::Insert( Node<T>* pos, const T&value)
{
	if (pos == nullptr)
	{
		cout << "插入位置为空" << endl;
	}
	else
	{
		// 插入到后面
		Node<T>* temp = _BuyNode(value);
		pos->_pPre->_pNext = temp;
		temp->_pPre = pos->_pPre;
		temp->_pNext = pos;
		pos->_pPre = temp;
	
		/*  // 插入到后面
		temp->_pNext = pos->_pNext;
		pos->_pNext->_pPre = temp;
		pos->_pNext = temp;
		temp->_pPre = pos;*/
		++_size;
	}
}
 
// 删除
template<typename T>
void DoubleLinkList<T>::Erase(Node<T>* pos)
{
	if (pos == nullptr)
	{
		cout << "删除失败，位置为空" << endl;
	}
	else
	{
		pos->_pPre->_pNext = pos->_pNext;
		pos->_pNext->_pPre = pos->_pPre;
		--_size;
	}
}
 
// 首元素 
template<typename T>
T& DoubleLinkList<T>::Front()
{
	return _pHead->_value;
};
 
template<typename T>
const T& DoubleLinkList<T>::Front()const
{
	return _pHead->_value;
};
 
// 尾元素 
template<typename T>
T& DoubleLinkList<T>::Back()
{
	return _pTail->_value;
};
 
template<typename T>
const T& DoubleLinkList<T>::Back()const
{
	return _pTail->_value;
};
 
 
// 判空
template<typename T>
bool DoubleLinkList<T>::Empty()const
{
	return 0 == _size;
}
 
// 元素个数
template<typename T>
size_t DoubleLinkList<T>::Size()const
{
	return _size;
}

